package com.orangecy.flowengine.flow;

import com.orangecy.common.util.ReflectionUtils;
import com.orangecy.common.util.SpringContextHolder;
import com.orangecy.flowengine.component.IComponentService;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author : orangeCy.奕
 * @Time : 2021/9/12
 * @desc : 猎豹养成产品源码
 * 流程定义
 */
@Component
public class FlowEngine {

    @SuppressWarnings("unchecked")
    private void doExecute(EngineContext context) {
        //获取流程身份定义
        FlowDefinition flowDefinition = FlowManager.getFlowDefine(context.getFlowId());
        FlowPhaseEnum.getFlowPhasesOrder().forEach(phase -> {
            //注入当前流程
            context.setFlowPhase(phase);
            //获取当前流程，所有组件集合
            List<String> components = (List<String>) ReflectionUtils.getFieldValue(phase.getName(), flowDefinition);
            if(components == null || components.size() == 0) {
                return;
            }
            components.forEach(p -> {
                IComponentService iComponentService = SpringContextHolder.getBean(p, IComponentService.class);
                iComponentService.execute();
            });
        });
    }
}